//
//  ATTemplateManager.m
//  AutomationTool
//
//  Created by Laurie Marmon on 3/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ATTemplateManager.h"
#import "ATScriptStepTemplate.h"
#import "ATScriptStepParameter.h"

#pragma mark Private Properties

@interface ATTemplateManager ()

@property (nonatomic, strong) NSMutableDictionary *scriptStepTemplates;

@end

#pragma mark Main Class Implementation
@implementation ATTemplateManager

static ATTemplateManager *sharedTemplateManager = nil;

@synthesize scriptStepTemplates = _scriptStepTemplates;

+ (ATTemplateManager*)sharedManager
{
    if (sharedTemplateManager == nil) {
        sharedTemplateManager = [[super allocWithZone:NULL] init];
        sharedTemplateManager.scriptStepTemplates = [[NSMutableDictionary alloc] init];
        [sharedTemplateManager loadScriptStepTemplates];
    }
    return sharedTemplateManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [self sharedManager];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (ATScriptStepTemplate *)templateWithName:(NSString *)name
{
    return [self.scriptStepTemplates objectForKey:name];
}

- (NSArray *)templateNames
{
    return [self.scriptStepTemplates allKeys];
}

- (NSArray *)allTemplates
{
    NSSortDescriptor *discriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    NSArray *templates = [self.scriptStepTemplates allValues];
    return [templates sortedArrayUsingDescriptors:[NSArray arrayWithObject:discriptor]];
}

- (void) loadScriptStepTemplates
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ScriptStepTemplates"
                                                          ofType:@"plist"];
    
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListImmutable
                                          format:&format
                                          errorDescription:&errorDesc];
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %lu", errorDesc, format);
    }
    
    //transform dictionary to objects
    NSArray *templates = [temp objectForKey:@"templates"];
    
    for (NSDictionary *dictionary in templates) {
        ATScriptStepTemplate *template = [[ATScriptStepTemplate alloc] init];
        NSMutableArray *tempParamArray = [[NSMutableArray alloc] init];
        for (NSString *key in dictionary) {
            if ([key isEqualToString:@"parameters"]) {
                NSArray *parameters = [dictionary objectForKey:key];
                for (NSDictionary *paramDictionary in parameters) {
                    ATScriptStepParameter *parameter = [[ATScriptStepParameter alloc] init];
                    for (NSString *paramKey in paramDictionary) {
                        [parameter setValue:[paramDictionary objectForKey:paramKey] forKey:paramKey];
                    }
                    [tempParamArray addObject:parameter];
                }
            } else {
                NSString *value = [dictionary objectForKey:key];
                [template setValue:value forKey:key];
            }
            
        }
        template.parameters  = [[NSArray alloc] initWithArray:tempParamArray];
        [self willChangeValueForKey:@"scriptStepTemplates"];
        [self.scriptStepTemplates setObject:template forKey:template.name];
        [self didChangeValueForKey:@"scriptStepTemplates"];
    }
    
}
@end
